<template>
  <el-button v-if="props.isButton === true" type="primary" @click="show">{{ props.title }}</el-button>
  <el-input v-if="props.isButton === false" :style="{ width: '200px' }" readonly v-model="pd.inputText"
    :placeholder="props.title">
    <template #prepend>
      <el-button icon="Search" size="small" @click="show"></el-button>
    </template>
  </el-input>

  <!-- 字典搜索对话框 -->
  <el-dialog :title="props.title" v-model="pd.open" width="900px" append-to-body>
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="数据查询" prop="dictLabel">
        <el-input v-model="pd.queryParams.name" placeholder="请输入名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
        <el-input v-model="pd.queryParams.mobile" placeholder="请输入手机号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table v-loading="pd.loading" @select="select1" @select-all="selectAll" ref="refTable" :data="pd.partnerList"
        height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="手机号" align="center" prop="mobile" />
        <el-table-column label="邮箱" align="center" prop="email" />
        <el-table-column label="社会编码" align="center" prop="idCard" />
        <el-table-column label="诞生日期" align="center" prop="birthday" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="创建人" align="center" prop="createBy" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人" align="center" prop="updateBy" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
        v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSelectd">确 定</el-button>
        <el-button @click="pd.open = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="PartnerSearch">
import { listPartner } from "@/api/hrp/partner";
import { getCurrentInstance, reactive } from "vue";
const { proxy } = getCurrentInstance();

const props = defineProps({
  // 字典名称
  title: {
    type: String,
    default: "合作伙伴联系人"
  },
  // 是否单选
  isSingle: {
    type: Boolean,
    default: true
  },
  inputText: {
    type: String
  },
  isButton: {
    type: Boolean,
    default: false
  },
  // 数据参数
  partner: {
    type: Object,
    default: null
  }
});

let pd = reactive({
  partnerList: [],
  open: false,
  loading: true,
  total: 0,
  inputText: "",
  reParams: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    mobile: undefined
  }
})

watch(props.inputText, val => {
  console.log(val);
  pd.inputText = val
}, { deep: true, immediate: true });
// 显示弹框
function show() {
  pd.queryParams = props.partner;
  getList();
  pd.open = true;
}

function select1(selection, row) {
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    proxy.$refs["refTable"].clearSelection();
  }
  //将这行选中
  proxy.$refs["refTable"].toggleRowSelection(row, true);
}
function selectAll(selection) {
  console.log(selection);
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    proxy.$refs["refTable"].clearSelection();
  }
}
// 查询表数据
function getList() {
  pd.loading = true;
  listPartner(pd.queryParams).then(res => {
    pd.partnerList = res.rows;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

defineExpose({
  show,
});
const emit = defineEmits(["selectd"]);

/** 选择操作 */
function handleSelectd() {
  pd.reParams = proxy.$refs["refTable"].getSelectionRows();
  if (pd.reParams.length == 0) {
    proxy.$modal.msgError("请选择数据，至少一行!!");
    return;
  }
  // inputText = reParams.map(item => item.name);
  pd.reParams.forEach(function (item) {
    console.log(item);
    pd.inputText = item.name;
  });
  pd.open = false;
  emit("selectd", pd.reParams);
}

</script>